package website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.constants.Code;
import website.domain.Message;
import website.domain.ResModel;
import website.domain.WebInfo;
import website.mapper.MessageMapper;
import website.mapper.WebMapper;
import website.service.MessageService;
import website.service.WebService;
import website.utils.NowDateUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Autowired
    private WebService webService;
    /**
     * 发布留言
     * @param message
     * @return
     */
    @Override
    public ResModel issue(Message message) {
        message.setIssueTime(NowDateUtils.getNowDate());
        boolean save = save(message);
        //更新网站留言量
        webService.update().setSql("message_count = message_count + 1").update();
        return ResModel.success(Code.ISSUE,save);
    }

    /**
     * 查询留言
     * @return
     */
    @Override
    public ResModel<List<Message>> getList() {
        //更新网站浏览量
        webService.update().setSql("visits = visits + 1").update();
        return ResModel.success(Code.SUCCESS,list());
    }

    /**
     * 分页查询留言信息
     * @param current
     * @param size
     * @return
     */
    @Override
    public ResModel<IPage<Message>> getByPage(Integer current, Integer size) {
        Page<Message> page = new Page<>(current, size);
        Page<Message> messagePage = messageMapper.selectPage(page, null);
        return ResModel.success(Code.SUCCESS,messagePage);
    }

    /**
     * 删除留言信息
     * @param id
     * @return
     */
    @Override
    public ResModel delMsg(Integer id) {
        if (id!=null){
            boolean b = removeById(id);
            //更新网站留言量
            webService.update().setSql("message_count = message_count - 1").update();
            return ResModel.success(Code.DELETE,b);
        }
        return ResModel.error(Code.FAIL);
    }
}
